home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 3D / RAVE Starter Samples / Texture Sample / Texture Triangle.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.1 KB  |  204 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Texture Triangle.cp
  3.  
  4.     Contains:    Much like the Gouraud shaded triangles sample, this one demonstrates
  5.                 texture mapping.
  6.  
  7.  
  8.     Written by: Timothy Carroll    
  9.  
  10.     Copyright:    Copyright © 1998-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 7/15/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include <RAVE.h>
  26. #include "Common Stuff.h"
  27. #include "RAVE Utilities.h"
  28. #include <Fonts.h>
  29. #include <Windows.h>
  30. #include <Menus.h>
  31. #include <TextEdit.h>
  32. #include <Dialogs.h>
  33.  
  34.  
  35. void main (void);
  36. void DoRAVEWindow(void);
  37.  
  38.  
  39. void main (void)
  40. {
  41.     // do standard mac init
  42.     InitGraf(&qd.thePort);
  43.     InitFonts();
  44.     InitWindows();
  45.     InitMenus();
  46.     TEInit();
  47.     InitDialogs(nil);
  48.     InitCursor();
  49.  
  50.     MaxApplZone();
  51.     MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
  52.  
  53.     // Initialize the SIN and COS lookup tables (see RAVE Utilities.cp)
  54.     if (InitializeLookups() == noErr)
  55.         DoRAVEWindow();
  56. }
  57.  
  58. void DoRAVEWindow(void)
  59. {
  60.     // These hold our errors
  61.     OSErr            theErr = noErr;
  62.     TQAError        theQAErr = kQANoErr;
  63.     
  64.     // These hold the window and device we'll put on the screen
  65.     GDHandle        device = NULL;
  66.     WindowRef        theWindow = NULL;
  67.     
  68.     // We try to center the window on the device.
  69.     Rect            bounds = {0,0,320,320};
  70.     Rect            deviceRect;
  71.     
  72.     // We use these parameters to describe our environment to RAVE
  73.     TQADevice        qaDevice;
  74.     TQARect         qaBoundsRect;
  75.     
  76.     // we get these items back from RAVE
  77.     TQAEngine         *theEngine = NULL;
  78.     TQADrawContext    *theContext = NULL;
  79.     
  80.     // We use these in our drawing loop to create our RAVE animation.
  81.     int             loop = 0;
  82.     TQATexture         *texture = NULL;
  83.     TQAVTexture     theTriangle[3];
  84.     
  85.     RgnHandle        gray;
  86.     Rect            grayRect;
  87.     
  88.     
  89.     // First, grab the first monitor with the deepest display
  90.     gray = GetGrayRgn();
  91.     grayRect = (**gray).rgnBBox;
  92.     device = GetMaxDevice(&grayRect);
  93.     FAIL_NIL (device, "\pERROR: No monitors found")
  94.     
  95.     
  96.     // Center our bounds in the middle of the screen.
  97.     deviceRect = (**device).gdRect;
  98.  
  99.     OffsetRect (&bounds,
  100.                     (deviceRect.right+deviceRect.left-bounds.right)/2 ,
  101.                     (deviceRect.bottom+deviceRect.top-bounds.bottom)/2);
  102.     
  103.     // Create a Window to hold the RAVE information.
  104.         
  105.     theWindow = NewCWindow(NULL, &bounds, "\pRAVE Window", true, zoomDocProc, (WindowRef) -1, false, 0);
  106.     FAIL_NIL (theWindow, "\pERROR: Couldn't create RAVE Window")
  107.     
  108.     SetPortWindowPort (theWindow);
  109.     
  110.     // Next we need to find a RAVE engine and then build a DrawContext to draw into it.  First, we'll find
  111.     // an engine that is capable of drawing to the selected GDevice.
  112.  
  113.     qaDevice.deviceType = kQADeviceGDevice;
  114.     qaDevice.device.gDevice = device;
  115.     
  116.     // We have to explicitly enable the Apple hardware card because it isn't 100% RAVE compliant.
  117.     // If we use this engine, we have to write special case code to deal with it.
  118. #if qEnableAppleHardware
  119.     QAEngineEnable (kQAVendor_Apple,kQAEngine_AppleHW);
  120. #endif
  121.     
  122.     // We'll just grab the first engine that the system offers us.
  123.     theEngine = FindTextureMappingEngine (&qaDevice);
  124.     FAIL_NIL (theEngine, "\pERROR:  No RAVE engines available that can texture map.")
  125.     
  126.     // Next we need to set up the a QARect with the Window's rectangle in GDevice local coordinates.
  127.     
  128.     qaBoundsRect.left = bounds.left - deviceRect.left;
  129.     qaBoundsRect.right = bounds.right  - deviceRect.left;
  130.     qaBoundsRect.top = bounds.top - deviceRect.top;
  131.     qaBoundsRect.bottom = bounds.bottom - deviceRect.top;
  132.  
  133.     // If we wanted to create a clipping region, we'd initialize a clip here.
  134.     
  135.     // Finally, we're ready to create the draw context!
  136.     theQAErr = QADrawContextNew (&qaDevice, &qaBoundsRect, NULL, theEngine, 
  137.                                  kQAContext_DeepZ | kQAContext_DoubleBuffer, &theContext);
  138.     if (theQAErr != kQANoErr) SIGNAL_ERROR ("\pERROR:  Failed to create RAVE draw context")
  139.     FAIL_NIL (theContext, "\pERROR:  Failed to create RAVE draw context")
  140.     
  141.     // set the background to black
  142.     QASetFloat (theContext, kQATag_ColorBG_a, 1.0);
  143.     QASetFloat (theContext, kQATag_ColorBG_r, 0.0);
  144.     QASetFloat (theContext, kQATag_ColorBG_g, 0.0);
  145.     QASetFloat (theContext, kQATag_ColorBG_b, 0.0);
  146.     
  147.     // Create the texture we'll use to animate our triangle.
  148.     texture = LoadTextureFromPictResource (theEngine, 128);
  149.     FAIL_NIL (texture, "\pERROR: Failed to load the texture from the resource")
  150.     
  151.     while (!Button())
  152.     {
  153.     theTriangle[0].x = 160 + 150*gCosArray[loop];
  154.     theTriangle[0].y = 160 + 150*gSinArray[loop];
  155.     theTriangle[0].z = 0.25;
  156.     theTriangle[0].invW = 1.0;
  157.     theTriangle[0].a = 1.0;
  158.     theTriangle[0].r = 0.0;
  159.     theTriangle[0].g = 0.0;
  160.     theTriangle[0].b = 0.0;
  161.     theTriangle[0].uOverW = 0.5;
  162.     theTriangle[0].vOverW = 0;
  163.     
  164.     theTriangle[1].x = 160 + 150*gCosArray[(loop+240) % 720];
  165.     theTriangle[1].y = 160 + 150*gSinArray[(loop+240) % 720];
  166.     theTriangle[1].z = 0.25;
  167.     theTriangle[1].invW = 1.0;
  168.     theTriangle[1].a = 1.0;
  169.     theTriangle[1].r = 0.0;
  170.     theTriangle[1].g = 0.0;
  171.     theTriangle[1].b = 0.0;
  172.     theTriangle[1].uOverW = 0;
  173.     theTriangle[1].vOverW = 1;
  174.     
  175.     theTriangle[2].x = 160 + 150*gCosArray[(loop+480) % 720];
  176.     theTriangle[2].y = 160 + 150*gSinArray[(loop+480) % 720];
  177.     theTriangle[2].z = 0.25;
  178.     theTriangle[2].invW = 1.0;
  179.     theTriangle[2].a = 1.0;
  180.     theTriangle[2].r = 0.0;
  181.     theTriangle[2].g = 0.0;
  182.     theTriangle[2].b = 0.0;
  183.     theTriangle[2].uOverW = 1;
  184.     theTriangle[2].vOverW = 1;
  185.  
  186.     QARenderStart (theContext, NULL, NULL);
  187.     QASetPtr (theContext, kQATag_Texture, texture);
  188.     QADrawTriTexture (theContext, &theTriangle[0], &theTriangle[1], &theTriangle[2], kQATriFlags_None);
  189.     QARenderEnd(theContext, NULL);
  190.     
  191.     // Increment our loop position    
  192.     loop = (loop+5) % 720;
  193.     }
  194.  
  195.  
  196.     error:
  197.     if (texture != NULL)
  198.         QATextureDelete (theEngine, texture);
  199.     if (theContext != NULL)
  200.         QADrawContextDelete (theContext);
  201.     if (theWindow != NULL)
  202.         DisposeWindow(theWindow);
  203.     return;
  204. }